using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace LS.GameKit
{
    public class LightControl : ControlBehaviour
    {

        [SerializeField]
        private bool m_IsOn = true;

        [SerializeField]
        private List<Light> m_Lights = new List<Light>();

        public UnityEvent OnLightsOpen;
        public UnityEvent OnLightsClose;

        public bool IsOn
        {
            get
            {
                return m_IsOn;
            }

            set
            {
                m_IsOn = value;
            }
        }

        public Light[] AllLights => m_Lights.ToArray();

        protected override void Start()
        {
            base.Start();
            if(m_Lights.Count > 0)
            {
                foreach (var item in m_Lights)
                {
                    item.enabled = m_IsOn;
                }
            }
        }

        public override void Preform(params object[] args)
        {
            if(args == null || args.Length == 0)
                SetIsOn(!m_IsOn);
            else if(args[0] is bool inputIsOn)
                SetIsOn(inputIsOn);
        }

        public void Open()
        {
            SetIsOn(true);
        }

        public void Close()
        {
            SetIsOn(false);
        }

        public void SetIsOn(bool isOn)
        {
            if (m_IsOn == isOn) return;

            m_IsOn = isOn;

            for (int i = 0; i < m_Lights.Count; i++)
                m_Lights[i].enabled = isOn;
            
            if (isOn)
                OnLightsOpen.Invoke();
            else
                OnLightsClose.Invoke();
        }

        public void SetIsOnWithoutNotify(bool isOn)
        {
            if (m_IsOn == isOn) return;

            m_IsOn = isOn;

            for (int i = 0; i < m_Lights.Count; i++)
                m_Lights[i].enabled = isOn;
        }
    }
}
